﻿using System.Linq;
using AutoMapper;
using MediatR;
using Rabbit.Domain;
using Rabbit.ErCode;
using Rabbit.lnfarnum;
using Rabbit.Write.API.WriteRepository.Commder;

namespace Rabbit.Write.API.WriteRepository.CommdHnader
{
    public class AddMerchanCommdHnder : IRequestHandler<AddMerchanCommder, APIResult<string>>
    {

        private readonly IBaseRepository<MerchandiseMangem> baseRepository;


        private readonly IMapper mapper;

        public AddMerchanCommdHnder(IBaseRepository<MerchandiseMangem> baseRepository,IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<string>> Handle(AddMerchanCommder request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();


            result.Code =ReCode.添加成功;
            result.Mage =ReCode.添加成功.ToString();


            var list = mapper.Map<MerchandiseMangem>(request);
            list.Complete = 80;
           

            var r = baseRepository.GetAll().Where(x => x.MerchanMid == request.MerchanMid).FirstOrDefault();
            if (r != null)
            {
                result.Code = ReCode.该商品已经存在;
                result.Mage = ReCode.该商品已经存在.ToString();
                return await Task.FromResult(result);
            }
            await baseRepository.Add(list);


            return await Task.FromResult(result);
        }
    }
}
